﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class ConfigurationNodesCollection : IConfigurationNodesCollection
    {
        List<IConfigurationNode> list = null;

        public ConfigurationNodesCollection(List<IConfigurationNode> list)
        {
            this.list = list;
        }

        public void Add(IConfigurationNode node)
        {
            if (node == null)
                throw new Exception("missing node.");

            string id = node.NodeName;

            if (string.IsNullOrEmpty(id))
                throw new Exception("missing id in node.");

            list.Add(node);
        }

        public IConfigurationNodeCollection this[string nodeName]
        {
            get
            {
                if (string.IsNullOrEmpty(nodeName))
                    throw new Exception("missing input nodeName");

                nodeName = nodeName.Trim().ToUpper();

                List<IConfigurationNode> nodes = new List<IConfigurationNode>();

                foreach (IConfigurationNode node in list)
                {
                    if (string.Equals(node.NodeName, nodeName, StringComparison.OrdinalIgnoreCase))
                        nodes.Add(node);
                }

                return new ConfigurationNodeCollection(nodes);
            }
        }

        public override string ToString()
        {
            return "nodes.count = " + list.Count;
        }

        public int Count
        {
            get
            {
                return list.Count;
            }
        }
    }
}
